﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSTSCLib;
using file;
using System.Reflection;

namespace DMSMT.Tool
{
    public partial class RDPTestFrm : Form
    {
        List<List<string>> rdpList = null;

        public RDPTestFrm()
        {
            InitializeComponent();
        }

        private void RDPTestFrm_Load(object sender, EventArgs e)
        {
            ExcelReader er = new ExcelReader();
            er.StratRow = 2;
            er.StratCol = 1;
            string path = Assembly.GetExecutingAssembly().Location;
            path = path.Substring(0, path.LastIndexOf("\\") + 1) + "rdp.xls";
            Log.LogText("读取远程服务器列表:\t" + path);
            rdpList = er.read(path);
        }

       
        private void btAdd2List_Click(object sender, EventArgs e)
        {
            try
            {
                Log.LogText("rdp7.Connected" + rdp7.Connected.ToString());
                rdp7.Server = tbServer.Text;  //乐博网提示  这里是输入服务器的ip地址或者域名
                rdp7.UserName = tbUserName.Text;   // 这里是输入服务器的登录用户 比如administrator
               
                IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp7.GetOcx();
                secured.ClearTextPassword = tbPwd.Text;  //这里输入服务器密码
                //rdp7.AdvancedSettings7.EnableCredSspSupport = true;
                //rdp7.AdvancedSettings8.NegotiateSecurityLayer = false;
                //rdp6.AdvancedSettings2.ClearTextPassword = tbPwd.Text;
                //rdp6.AdvancedSettings2.ConnectToServerConsole = true;

                rdp7.Connect();
                //rdpr8.Show();
                //rdpr8.FullScreen = true;
                //rdpr8.FullScreenTitle = this.Text;

                if (rdp7.Connected.ToString() == "1")
                    Log.LogText("成功连接远程桌面"+tbServer.Text);
                else
                    Log.LogText("连接远程桌面" + tbServer.Text + "失败\t" + rdp7.Connected.ToString());
                Log.LogText("rdp1.Connected" + rdp7.Connected.ToString());
                Log.LogText(rdp7.ConnectingText);
            }
            catch (Exception Ex)
            {
                MessageBox.Show("连接错误", "不能连接到远程桌面 " + tbServer.Text + " 错误信息:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            try
            {
                Log.LogText("rdp7.Connected" + rdp7.Connected.ToString());
                // Check if connected before disconnecting
                if (rdp7.Connected.ToString() == "1")
                {
                    rdp7.Disconnect();
                    Log.LogText("成功断开远程桌面" + tbServer.Text);
                }
                else
                {
                    Log.LogText("远程桌面已断开");
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show("断开连接失败", "不能断开远程桌面连接 " + tbServer.Text + " 错误信息:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbServer_Leave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbServer.Text))
            {
                return;
            }
            for (int i = 0; i < rdpList.Count; i++)
            {
                List<string> row = rdpList[i];
                if (row[3].Trim() == tbServer.Text.Trim())
                {
                    tbUserName.Text = row[6];
                    tbPwd.Text = row[7];
                    break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AxMSTSCLib.AxMsRdpClient7 rdpClient = new AxMSTSCLib.AxMsRdpClient7();
            rdpClient.Dock = DockStyle.Fill;


            rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;//控件宽度   
            rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;//控件宽度   
            this.Controls.Add(rdpClient);
            rdpClient.Server = tbServer.Text;//服务器地址   
            rdpClient.UserName = tbUserName.Text;
            
            rdpClient.AdvancedSettings2.RDPPort = 3389;
            rdpClient.AdvancedSettings2.ClearTextPassword = tbPwd.Text; //密码   
            //rdpClient.AdvancedSettings6
            //rdpClient.ColorDepth = 16;//颜色位数   
            //rdpClient.FullScreen = true;//是否全屏   
            //rdpClient.AdvancedSettings8.PublicMode = true;
            rdpClient.AdvancedSettings7.EnableCredSspSupport = true;
            rdpClient.AdvancedSettings6.ClearTextPassword = tbPwd.Text;

            rdpClient.Connect();  
        }
    }
}
